package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
 
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;
 
import javax.crypto.SecretKey;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import code.KeyStoreManager;
import code.Language;
import code.LogEncryption;
import code.Settings;


public class DataIntegrityKeyList extends JPanel {
	
	Color c= new Color(0x80, 0x80, 0xFF);

	private static final long serialVersionUID = 1L;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JLabel jLabel = null;
	ATOCOmain atocoframe;
	private KeyStoreManager ksm;
	private Settings s;

	private JLabel jLabel1 = null;
	LogEncryption log=new LogEncryption();
	
	DataIntegrityCreateFile prevclass;
	private HashMap labels;
	/**
	 * This is the default constructor
	 */
	public DataIntegrityKeyList(ATOCOmain atocoframe, DataIntegrityCreateFile prevclass) {
		super();
		Language l = new Language();
		s= new Settings();
		labels = l.changeLanguage((String) s.global.get("language"), "DataIntegrityKeyList", (String) s.global.get("installPath"));

		this.prevclass=prevclass;
		
		// Every Keystore operation must use this one.
		ksm = new KeyStoreManager(
				(String)Settings.global.get("personalKeyStoreFile"), 
				(String)Settings.global.get("personalKeyStorePassword"));
		if (ksm.getKeyStore() == null) {
        	System.out.println("Keystore file not found. Creating one.");
        	ksm.createKeyStore(
        			(String)Settings.global.get("personalKeyStoreFile"), 
    				(String)Settings.global.get("personalKeyStorePassword"));
        }
		log.initiateKeylist();
		
		this.atocoframe=atocoframe;
		initialize();
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(532, 114, 127, 39));
		jLabel1.setText((String) labels.get("jLabel1"));
		jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				System.out.println(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());
//				JPasswordField jpf = new JPasswordField();
//				int inputText = JOptionPane.showConfirmDialog(null, jpf,
//				  "Password:", JOptionPane.OK_CANCEL_OPTION);
				
				JLabel label = new JLabel((String) labels.get("label"));
				JPasswordField jpf = new JPasswordField();
				JOptionPane.showConfirmDialog(null,
				  new Object[]{label, jpf}, "Password:",
				  JOptionPane.OK_CANCEL_OPTION);

				if(!jpf.getText().equals("")){
					SecretKey key=ksm.getSecretKey(jTable.getValueAt(jTable.getSelectedRow(), 0).toString(), jpf.getText());
					prevclass.mc.setKey(key);
					prevclass.mc.algorithm=jTable.getValueAt(jTable.getSelectedRow(), 1).toString();
					prevclass.mc.keyalias=jTable.getValueAt(jTable.getSelectedRow(), 0).toString();
					atocoframe.contentPane(prevclass);
				}
			}
		});
		jLabel = new JLabel();
		jLabel.setBounds(new Rectangle(61, 17, 127, 16));
		jLabel.setText((String) labels.get("jLabel"));
		jLabel.setForeground(c);
		jLabel.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
		this.setSize(764, 417);
		this.setLayout(null);
		this.add(getJScrollPane(), null);
		this.setBackground(Color.WHITE);
		this.add(jLabel, null);
		this.add(jLabel1, null);
	}

	
	
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(60, 75, 441, 281));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			
			Vector<Object> rowData = new Vector<Object>();
			
			HashSet<String> aliases = ksm.listAllKeys();
			Iterator it = aliases.iterator();
			while(it.hasNext()) {
				String al = (String) it.next();
				if (ksm.getPublicKey(al) == null) {
	        		// No certificate entry so it must be secretKey
	        		System.out.println("Secret key");
	            	Vector<Object> row = new Vector<Object>();
	            	row.add(al);
	            	System.out.println(log.keylist);
	            	row.add(log.keylist.get(al));
					
	            	rowData.add(row);
			}}
			
			Vector<String> columnNames = new Vector<String>();
			columnNames.add((String) labels.get("tab1"));
			columnNames.add((String) labels.get("tab2"));

			
			if (!rowData.isEmpty()) {
				DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
			
			jTable = new JTable(model);
			

		}
		
		}
		return jTable;
}
}



  //  @jve:decl-index=0:visual-constraint="10,10"